package com.df.bpm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.df.common.service.impl.BaseServiceImpl;
import com.df.bpm.entity.InformatizationReason;
import com.df.bpm.mapper.InformatizationReasonMapper;
import com.df.bpm.service.InformatizationReasonService;

import com.df.common.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 需求原因服务
 */
@Service
@RequiredArgsConstructor
public class InformatizationReasonServiceImpl extends BaseServiceImpl<InformatizationReasonMapper,InformatizationReason> implements InformatizationReasonService {
	private final InformatizationReasonMapper informatizationReasonMapper;

    public void saveReasons4RequirementId(Integer requirementId, List<InformatizationReason> reasons) {
        StringBuilder reasonsId= null;
        for(InformatizationReason reason:reasons){
            if(CommonUtils.varIsNotBlank(reason.getInformatizationReasonIndex())){
                if(reasonsId==null){
                    reasonsId = new StringBuilder("" + reason.getInformatizationReasonIndex());
                }else{
                    reasonsId.append(",").append(reason.getInformatizationReasonIndex());
                }
            }
        }
        if(CommonUtils.varIsNotBlank(reasonsId)){
            informatizationReasonMapper.delete4Save(requirementId,reasonsId.toString());
        }else{
            informatizationReasonMapper.delete(new QueryWrapper<InformatizationReason>().eq("informatizationRequirementIndex",requirementId));
        }
        for(InformatizationReason reason:reasons){
            if(CommonUtils.varIsNotBlank(reason.getInformatizationReasonIndex())){
                if(CommonUtils.varIsNotBlank(reason.getDefectNow()) &&
                        !"-".equals(reason.getDefectNow())) {
                    informatizationReasonMapper.updateById(reason);
                }else{
                    informatizationReasonMapper.deleteById(reason);
                }
            }else{
                if(CommonUtils.varIsNotBlank(reason.getDefectNow())){
                    reason.setInformatizationRequirementIndex(requirementId);
                    informatizationReasonMapper.insert(reason);
                }
            }
        }
    }
}